<?php

class import {

    public static final function importFolderFiles($fileLocation) {
        $locFile = $fileLocation; // Path Location of the file to be uploaded in the system.
        $destdir = $locFile;

        //check if folder location is empty
        $handle = opendir($destdir);
        $c = 0;
        while ($file = readdir($handle) && $c < 3) {
            $c++;
        }

        if ($c > 2) {
            $dirs = scandir($locFile);
            foreach ($dirs as $filename) {

                if (($filename == '.') || ($filename == '..')) {
                    
                } elseif (is_dir($locFile . $filename)) {
                    //filesInDir($locFile.'|'.$filename);
                } else {

                    //$location = $locFile.'/'.$file; // Read the file Location
                    $extension = explode(".", $filename);


                    if ($extension[1] == "csv") {
                        $location = $locFile . $filename;
                        $loop = 0;
                        $fp = @fopen($location, 'r');

                        while (!feof($fp)) {

                            $dest = $_POST['location'];
                            $return_data = array();
                            while (($data = fgetcsv($fp, 1000, ",")) !== FALSE) {
                                array_push($return_data, $data);
                            }

                            $loc = $locFile;
                            $dest = "C:/wamp/www/GS3/Tupperware/gs3_tupperware/images/products/";

                            //        copy($loc . $filename, $dest . $filename); // If we copied this successfully, mark it for deletion
                            $fp++;
                            return $return_data;
                        }

                        fclose($fp);
                        //   unlink($loc . $filename); // Delete all successfully-copied files
                    } else {
                        return "Invalid File Format.";
                    }
                }
            }
        } else {

            return $locFile . " Folder location is empty.";
        }
    }

    public static final function importCSV($location) {
        $loop = 0;
        $fp = @fopen($location, 'r');

        $return_data = array();
        while (!feof($fp)) {
            $dest = $_POST['location'];

            while (($data = fgetcsv($fp, 1000, ",")) !== FALSE) {
                array_push($return_data, $data);
            }

            $fp++;
        }

        return $return_data;
        fclose($fp);
    }

}

?>